package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import com.hjq.base.BaseDialog
import com.hjq.fcloud.R
import com.hjq.fcloud.bean.CommonFileBean
import com.hjq.fcloud.camear.BaseCameraActivity
import com.hjq.fcloud.camear.BitmapSaver
import com.hjq.fcloud.http.api.FaceAuthApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.ui.dialog.MessageDialog
import com.hjq.fcloud.utils.BitmapUtils
import com.hjq.fcloud.utils.CommonUploadUtils
import com.hjq.fcloud.utils.ZipImageUtils
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback

/**
 * 人脸识别界面
 */
open class FaceDetectRGBActivity : BaseCameraActivity() {

    companion object {
        fun launcher(context: Context, bundle: Bundle?) {
            var intent = Intent(context, FaceDetectRGBActivity::class.java)
            if (bundle != null) {
                intent.putExtras(bundle)
            }
            context.startActivity(intent)
        }
    }

    /**
     * 人脸比对
     */
    public override fun handlerResultBmp(bitmap: Bitmap) {
        var fileName = System.currentTimeMillis().toString().plus(".png")
        var filePath = BitmapSaver.saveToInternalStorage(
            this,
            bitmap,
            fileName
        )
        ZipImageUtils().zipImages(
            context,
            filePath
        ) { zipSuccess: Boolean, errorMsg: String?, zipImgList: List<CommonFileBean>? ->
            if (zipSuccess) {
                var zipFilePath = zipImgList!!.get(0).url
                CommonUploadUtils.uploadFile(this, filePath, { success, errorMsg, items ->
                    if (success) {
                        var faceUrl = items!!.get(0).url
                        compareFace(faceUrl,zipFilePath)
                    } else {
                        showCompareFailDialog(errorMsg)
                    }
                }, false)
            } else {
                toast("图片压缩失败")
            }
        }
    }

    /**
     * 人脸比对
     */
    open fun compareFace(url: String, filePath: String) {
        var api = FaceAuthApi()
            .setBizId(commonJumpBean?.bizId)
            .setBizCode(commonJumpBean?.bizCode)
            .setAuth_pic(url)
            .setAuth_pic_base64(BitmapUtils.imageToBase64(filePath))
        EasyHttp.post(this)
            .api(api)
            .request(object : HttpCallback<HttpData<FaceAuthApi.Result>?>(this) {
                override fun onSucceed(data: HttpData<FaceAuthApi.Result>?) {
                    var token = data?.getData()?.auth_token
                    if (!TextUtils.isEmpty(token)) {
                        handlerFaceCompareSuccessOperation(token!!)
                    } else {
                        showCompareFailDialog("数据异常")
                    }
                }

                override fun onFail(e: Exception?) {
                    showCompareFailDialog(e?.message)
                }
            })
    }

    fun handlerFaceCompareSuccessOperation(token: String) {
        var bundle = getBundle()
        bundle?.putString(SignActivity.INTENT_OUT_FACE_AUTH_TOKEN, token)
        SignActivity.launcher(context, bundle)
        finish()
    }

    open fun showCompareFailDialog(msg: String?) {
        MessageDialog.Builder(activity)
            .setTitle(getString(R.string.string_dialog_default_title))
            .setMessage(msg)
            .setCancel("返回")
            .setConfirm("重试")
            .setCancelable(false)
            .setCanceledOnTouchOutside(false)
            .setListener(object : MessageDialog.OnListener {
                override fun onConfirm(dialog: BaseDialog?) {
                    continueScan()
                }

                override fun onCancel(dialog: BaseDialog?) {
                    super.onCancel(dialog)
                    finish()
                }
            })
            .show()
    }
}